﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Facade;
using IronOne.SalesOne.BusinessObjects;

namespace IronOne.SalesOne.SalesOneWeb.ContactManagement
{
    public partial class PolicyDetails : SOSecurityPage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            //string[] but = new string[] { "btnView", "btnCancel"};
            //UCGridButtons1.Buttons = but;
            //UCGridButtons1.SetCredentials(ValidateUserCredentials(Request.Url.LocalPath));
            try
            {
                if (!IsPostBack)
                {

                    if (Session["cont"] != null)
                    {
                        Session.Remove("cont");
                    }
                    if (!string.IsNullOrEmpty(Request.UrlReferrer.ToString()))
                    {
                        ViewState["refUrl"] = Request.UrlReferrer.ToString();
                    }

                    //if (!Page.IsPostBack && Request.UrlReferrer.ToString().Contains("AppointmentDetails.aspx"))
                        //Session["detailsQueryString"] = Request.UrlReferrer.Query.ToString();
                }


                ValidateUserCredentials(Request.Url.LocalPath);
                CustomerFacade cusFac = new CustomerFacade();

                string contractId = "";

                if (!string.IsNullOrEmpty(Request.QueryString["con"]))
                {
                    contractId = Request.QueryString["con"];
                }

                if (!string.IsNullOrEmpty(Request.QueryString["t"]))
                {
                    btnClient.Visible = false;
                }
                else
                {
                    btnClient.Visible = true;
                }


                /*if (Session["policyObj"] != null)
                {
                    GridViewRow polRow = (GridViewRow)Session["policyObj"];
                    
                    cus = cusFac.GetCustomer(Convert.ToInt32(polRow.Cells[3].Text));
                }
                else if (!string.IsNullOrEmpty(Request.QueryString["contract"]))
                {*/

                Customer cus = cusFac.GetCustomerByContractId(contractId);
                Policy policy = cusFac.GetPolicy(contractId);




                lblClientId.Text = cus.CustomerId;
                lblContractNo.Text = policy.ContractId;
                lblPlanNo.Text = policy.PlanCode;
                lblPolicyStatus.Text = policy.PolicyState;

                lblBalanceAmt.Text = String.Format("{0:0,0.00}", policy.Suspence);
                    //polRow.Cells[7].Text;

                //lblPremium.Text = string.Format("{0:c}", policy.Premium.ToString());
                lblPremium.Text = String.Format("{0:0,0.00}", policy.Premium);
                lblRiskDate.Text = policy.RiskDate;
                lblUpdateTime.Text = policy.LastUpdatedDate;
                lblSoCode.Text = policy.SOCode;
                lblPaymentMode.Text = policy.PayMode;
                lblSumAssured.Text = String.Format("{0:0,0.00}", policy.SumAssured);
                lblTerm.Text = policy.Term.ToString();
                lblCollectionDue.Text = String.Format("{0:0,0.00}", policy.CollectionDue); 
                lblPaidToDate.Text = policy.DueDate;
                lblDob.Text = cus.DOB;

                Session["client"] = cus;
            }
            catch (Exception ess)
            {
                
            }
            
            //}
        }

        protected void TextBox12_TextChanged(object sender, EventArgs e)
        {

        }

        protected void policyStatusTextBox_TextChanged(object sender, EventArgs e)
        {

        }

        

        protected void btnClient_Click(object sender, EventArgs e)
        {
            Response.Redirect("~/ContactManagement/ClientDetails.aspx");
        }

        protected void btnCancel_Click(object sender, EventArgs e)
        {
            try
            {
                string type = string.Empty;

                if (!string.IsNullOrEmpty(Request.QueryString["p"]))
                {
                    type = Request.QueryString["p"];

                    if (type.Equals("a"))
                    {
                        Response.Redirect("~/CustomerServices/AcknowledgeSlip.aspx?cs=ack");
                    }
                    if (type.Equals("p"))
                    {
                        Response.Redirect("~/ContactManagement/SearchContact.aspx");
                    }
                    /*if (type.Equals("s"))
                    {
                        Response.Redirect("~/SalesActivity/ActivityPlan.aspx" + Session["detailsQueryString"].ToString());
                        Session.Remove("detailsQueryString");
                    }*/
                }
                else
                {
                    if (ViewState["refUrl"] != null)
                    {
                        Response.Redirect(ViewState["refUrl"].ToString());


                    }
                }
            }
            catch (Exception)
            {
                
            }

        }
    }
}
